package com.smartleanx.module.hrm.service.salary;

import com.alibaba.excel.converters.AutoConverter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.module.hrm.dal.dataobject.dingDing.DingAttendanceTeam;
import com.smartleanx.module.hrm.dal.mysql.dingDing.DingAttendanceTeamMapper;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2025-02-10
 */

public class ConverterServiceImpl implements Converter<String> {

    private final Map<Long, String> dynamicMapping;
    private static final List<String> ALL_NAMES = new ArrayList<>();

   public ConverterServiceImpl(Map<Long, String> data){
       this.dynamicMapping = data;
   }

    // 1. 指定支持的Java类型
    @Override
    public Class<?> supportJavaTypeKey() {
        return String.class; // 必须与泛型类型一致
    }

    // 2. 指定支持的Excel单元格类型
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING; // 支持文本类型
    }


    @Override
    public String convertToJavaData(ReadConverterContext<?> context) {
        String id = context.getReadCellData().getStringValue();
        System.out.println("convertToJavaData:"+id);
        if(null == id){
            return "";
        }
        return dynamicMapping.get(Long.parseLong(id)); // 返回
    }

    // 导出时：ID → 姓名
    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<String> context) {
//        String value = context.getValue();
//        String name = NAME_TO_ID_MAP.get().entrySet().stream()
//                .filter(entry -> entry.getValue().equals(value))
//                .map(Map.Entry::getKey)
//                .findFirst()
//                .orElse("未知用户");
        System.out.println("convertToExcelData:"+context.getValue());
        if(null == context.getValue()){
            return new WriteCellData<>("");
        }
        return new WriteCellData<>(context.getValue());
    }
}
